package at.jku.ase.communication.parsers;

import at.jku.ase.communication.SessionObject;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;

public abstract class ComponentParser<T> {

    private final SessionObject session;
    private final XMLResponse action;

    public ComponentParser(final SessionObject session, final XMLResponse action) {
        this.session = session;
        this.action = action;
    }

    public void handle(XMLStreamReader xmlReader) {
        try {
            this.processRequest(xmlReader);
            action.response();
        } catch (XMLStreamException e) {
            throw new RuntimeException("XML Syntax error!", e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected abstract T processRequest(XMLStreamReader reader) throws XMLStreamException;

    public SessionObject getSession() {
        return session;
    }

}
